copying contents of a string to another string

Hello,

I want to perform a string copy. The objective is to copy the contents of one string to another without spaces.
I get an error with the following.

Anyone know how I can change this to get it to work?
string line = " RANDOM STRING WITH SPACES"
string new_string
int y

new_s= ""
for (y = 0; y< length(line); y++)

{

if (isspace(line[y])) continue
{
print line[y]
new_s= line[y]

}

}

regards

Colm
MyDeveloerWorks - Mon Nov 12 03:53:44 EST 2012

Re: copying contents of a string to another string
adevicq - Mon Nov 12 04:05:36 EST 2012

Hi,

This works:

string line = " RANDOM STRING WITH SPACES"
int y
string new_s= ""
 
for (y = 0; y< length(line); y++) {
    if (isspace(line[y])) 
                continue
 
        print line[y] 
        new_s += ("" line[y] "")
}


I assumed that

 

line[y] ""



Should convert the character into a string, but it does not seem to work...
In addition, you forgot the "+=" to concatenate the result.

Regards,
Alain



 

Re: copying contents of a string to another string
MyDeveloerWorks - Mon Nov 12 04:59:09 EST 2012

adevicq - Mon Nov 12 04:05:36 EST 2012

Hi,

This works:

string line = " RANDOM STRING WITH SPACES"
int y
string new_s= ""
 
for (y = 0; y< length(line); y++) {
    if (isspace(line[y])) 
                continue
 
        print line[y] 
        new_s += ("" line[y] "")
}


I assumed that

 

line[y] ""



Should convert the character into a string, but it does not seem to work...
In addition, you forgot the "+=" to concatenate the result.

Regards,
Alain



 

Hello Alain,,
The result I want (.ei if I do print new_s at the end) is a string that contains now spaces, no newline chars, i.e. "RANDOMSTRINGWITHSPACES".

After playing around with the strange looking space ' ' character that is used for concatenation, I got a result.
string line = " RANDOM STRING WITH SPACES"
int y
string new_s= ""

for (y = 0; y< length(line); y++) {
if (isspace(line[y]))
continue

new_s = new_s liney:y
}
print new_s
thanks for pointing me in the right direction!

Re: copying contents of a string to another string
MyDeveloerWorks - Mon Nov 12 05:02:31 EST 2012

MyDeveloerWorks - Mon Nov 12 04:59:09 EST 2012
Hello Alain,,
The result I want (.ei if I do print new_s at the end) is a string that contains now spaces, no newline chars, i.e. "RANDOMSTRINGWITHSPACES".

After playing around with the strange looking space ' ' character that is used for concatenation, I got a result.
string line = " RANDOM STRING WITH SPACES"
int y
string new_s= ""

for (y = 0; y< length(line); y++) {
if (isspace(line[y]))
continue

new_s = new_s liney:y
}
print new_s
thanks for pointing me in the right direction!

Sorry, I was missing square brackets in the loop assignment
new_s = new_s liney:y

Re: copying contents of a string to another string
llandale - Mon Nov 12 17:21:40 EST 2012

Don't concatenate inside a loop, use a buffer instead:

Buffer    bufStripSpaces = create()  // Local to following function
string  StripSpaces(string in_String)
{
        int     y
        char    c
        bufStripSpaces = ""
        for (y = 0; y< length(in_String); y++) {
           c = in_String[y]
           if (isspace(c) )             continue
 
           bufStripSpaces += c
        }
        return(stringOf(bufStripSpaces))
}     // end StripSpaces()
 
 
void    DoTest(string in_String)
{
        string Results = StripSpaces(in_String)
        print in_String "\t[" Results "]\n"
}
 
 
DoTest(" RANDOM STRING WITH SPACES")


Note that function "isspace(char)" catches these characters:

 

  • ' \t\n\v\f\r'

which is space, TAB, EOL, and MS-Word LF, page-break, and CR.

-Louie